package com.luych.toolbox.data.service.controller;

import com.luych.toolbox.common.constant.PageConstant;
import com.luych.toolbox.common.feign.intf.database.service.DataRecoveryHistoryFeign;
import com.luych.toolbox.common.feign.param.data.service.DataRecoveryHistoryParam;
import com.luych.toolbox.common.feign.view.data.service.DataRecoveryHistoryView;
import com.luych.toolbox.common.page.CommonPageParam;
import com.luych.toolbox.common.page.CommonPageResult;
import com.luych.toolbox.common.page.CommonSort;
import com.luych.toolbox.common.page.PageResult;
import com.luych.toolbox.common.service.base.BaseController;
import com.luych.toolbox.data.service.entity.DataRecoveryHistory;
import com.luych.toolbox.data.service.service.DataRecoveryHistoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@Api(tags = "数据备份历史")
@RestController
@RequestMapping("/drh")
public class DataRecoveryHistoryController
        extends BaseController<DataRecoveryHistoryView, DataRecoveryHistoryParam, DataRecoveryHistory>
        implements DataRecoveryHistoryFeign {

    private final DataRecoveryHistoryService dataRecoveryHistoryService;

    @Autowired
    public DataRecoveryHistoryController(DataRecoveryHistoryService dataRecoveryHistoryService) {
        super(dataRecoveryHistoryService);
        this.dataRecoveryHistoryService = dataRecoveryHistoryService;
    }

    @ApiOperation("按关键字查询数据代理信息")
    @GetMapping("/search/keyword")
    public CommonPageResult<DataRecoveryHistoryView> pageByKeyword(
            @ApiParam(value = "每页容量") @RequestHeader(name = "pageSize", required = false, defaultValue = PageConstant.DEFAULT_PAGE_SIZE_STR) long pageSize,
            @ApiParam(value = "查询页码") @RequestHeader(name = "pageNumber", required = false, defaultValue = PageConstant.DEFAULT_PAGE_NUMBER_STR) long pageNumber,
            @ApiParam(value = "排序字段") @RequestHeader(name = "sort", required = false, defaultValue = PageConstant.DEFAULT_PAGE_SORT) String sort,
            @ApiParam(value = "正序/倒序，取值ASC/DESC") @RequestHeader(name = "order", required = false, defaultValue = PageConstant.DEFAULT_PAGE_ORDER) String order,
            @ApiParam(value = "查询关键字，支持按名称模糊查询") @RequestParam(name = "keyword", required = false) String keyword
    ) {
        CommonPageParam commonPageParam = CommonPageParam.getInstance().setPageSize(pageSize).setPageNumber(pageNumber);
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            commonPageParam.addSort(CommonSort.getInstance().setSort(sort).setOrder(order));
        }
        PageResult<DataRecoveryHistoryView> pageResult = dataRecoveryHistoryService.pageByKeyword(commonPageParam, keyword);
        return CommonPageResult.getInstance(pageResult);
    }
}
